/**
 * 内容页面
 */

//图片前置url
var pre = imageUrl;

//从网址上拿参数
var urlParam = getQueryVariable();
var id = urlParam.id;
var parmType = urlParam.type;

//映射
var resource = [
    {type:'YWDT',title:'要闻动态',text:'这里有最新的军民融合相关资讯'},
    {type:'TPGG',title:'平台公告',text:'这里有最新的平台动态与活动'},
    {type:'TZGGHDTZ',title:'活动通知',text:'这里有最新的平台动态与活动'},
    {type:'JMRHZC',title:'军民融合政策',text:'军民融合的相关政策在这里汇聚'},
];

//给标题栏赋值
for(var i=0;i<resource.length;i++){
    if(resource[i].type == parmType){
        $('.z_banner_title').html(resource[i].title);
        $('.z_banner_text').html(resource[i].text);
    }
}

//给导航栏赋下标
if(parmType == 'YWDT'){
    $("ul[class='clearfix'] li:eq(1)").addClass('on');
    $('#baoti').html('要闻动态');
} else if(parmType == 'TPGG' || parmType == 'TZGGHDTZ'){
    $("ul[class='clearfix'] li:eq(2)").addClass('on');
    $('#baoti').html('通知公告');
} else {
    $("ul[class='clearfix'] li:eq(4)").addClass('on');
    $('#baoti').html('政策导向');
}

//内容页面
$(function () {

    /**
     * 获取preId和nextId
     * params：parmType
     */
    function getIds() {

        if(parmType !='TZGGHDTZ'){

            $.ajax({
                type: "GET",
                url:restUrl + "/index/items",   //接口地址
                dataType: "json",
                data: {
                    parentCode: parmType,
                    loadAll: false,
                    needPaging: false,
                },
                success: function (v) {
                    if (v.code == 10000) {
                        var idArray = [];
                        var titles = [];
                        for (var i = 0; i < v.data.rows.length; i++) {
                            var item = v.data.rows[i];
                            idArray.push(item.id);
                            titles.push(item.title);
                        }

                        var index = idArray.indexOf(id);
                        var preId = index - 1 >= 0 ? idArray[index - 1] : idArray[idArray.length - 1];
                        var nextId = index == idArray.length - 1 ? idArray[0] : idArray[index + 1];
                        var preTitle = titles[idArray.indexOf(preId)];
                        var nextTitle = titles[idArray.indexOf(nextId)];

                        getData(preId,nextId,preTitle,nextTitle);
                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            });

        } else {

            $.ajax({
                type: "GET",
                url: restUrl + "/index/exchanges",   //接口地址
                dataType: "json",
                data: {
                    loadAll: false,
                    needPaging: false,
                },
                success: function (v) {
                    if (v.code == 10000) {

                        var idArray = [];
                        var titles = [];
                        for (var i = 0; i < v.data.rows.length; i++) {
                            var item = v.data.rows[i];
                            idArray.push(item.id);
                            titles.push(item.title);
                        }

                        var index = idArray.indexOf(id);
                        var preId = index - 1 >= 0 ? idArray[index - 1] : idArray[idArray.length - 1];
                        var nextId = index == idArray.length - 1 ? idArray[0] : idArray[index + 1];
                        var preTitle = titles[idArray.indexOf(preId)];
                        var nextTitle = titles[idArray.indexOf(nextId)];

                        getData(preId,nextId,preTitle,nextTitle);
                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            });

        }


    };

   getIds();

    /**
     * 拿数据，画网页
     */
    function getData(preId,nextId,preTitle,nextTitle) {

        if(parmType !='TZGGHDTZ'){

            $.ajax({
                type: "GET",
                url: restUrl + "/index/article/" + id,   //接口地址
                dataType: "json",
                data: {},
                success: function (v) {
                    if (v.code == "10000") {

                        var item = v.data;
                        if(item.title.indexOf('\n') >=0){ item.title = item.title.replace(/\n/g,"<br/>");}
                        if(item.copied ==0){
                            var str = '<h1 class="animated fadeInDown" style="text-align: center">' + item.title + '</h1><h5 class="animated fadeInDown" style="text-align: center;margin-top:10px;margin-bottom:20px">发布时间：' + formatDate(item.publishDate) +
                                '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;作者：' + item.author + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;阅读量：' + item.clicks +'</h5>' +
                                '<div class="animated fadeInUp" style="animation-delay: 0.4s"><p>' + item.contentTxt + '</p><br/></div><div class="fj"></div><div class="link"><div class="link_1"><span>下一篇：</span>' +
                                '<a href="info.html?id=' + nextId + '&type=' + parmType + '">' + nextTitle + '</a></div><div class ="link_1"><span> 上一篇：</span>'+
                                '<a href="info.html?id=' + preId + '&type=' + parmType + '">' + preTitle + '</a></div></div>';
                            $('.news_del .main').html(str);
                        }else {
                            var str = '<h1 class="animated fadeInDown" style="text-align: center">' + item.title + '</h1><h5 class="animated fadeInDown" style="text-align: center;margin-top:10px;margin-bottom:20px">发布时间：' + formatDate(item.publishDate) +
                                '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;来源：' + item.copyFrom + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;阅读量：' + item.clicks +'</h5>' +
                                '<div class="animated fadeInUp" style="animation-delay: 0.4s"><p>' + item.contentTxt + '</p><br/></div><div class="fj"></div><div class="link"><div class="link_1"><span>下一篇：</span>' +
                                '<a href="info.html?id=' + nextId + '&type=' + parmType + '">' + nextTitle + '</a></div><div class ="link_1"><span> 上一篇：</span>'+
                                '<a href="info.html?id=' + preId + '&type=' + parmType + '">' + preTitle + '</a></div></div>';
                            $('.news_del .main').html(str);
                        }

                        if(item.cmsFileList.length != 0){
                            $('.fj').addClass('xline');
                            for(var i=0;i<item.cmsFileList.length;i++){
                                $('.fj').append('<br/><a href="' + pre + item.cmsFileList[i].savePath + '" download="' + item.cmsFileList[i].reqRelUrl + '"><span style="font-weight: bold">附件' + (i+1) + '：</span>' + item.cmsFileList[i].fileName + '</a>');
                            }
                        }


                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            })

            $.ajax({
                type: "GET",
                url: restUrl + "/index/items",   //接口地址
                dataType: "json",
                data: {
                    parentCode: parmType,
                    loadAll: false,
                    needPaging: false,
                },
                success: function (v) {
                    if (v.code == "10000") {
                        for(var i=0;i<v.data.rows.length;i++){
                            ;
                            var item = v.data.rows[i]
                            $('.dd').append('<li class="recommend"><a href="info.html?id=' + item.id + '&type=' + parmType + '">' +
                                '<div class="xx" style="width:30%;"><img src="' + pre + item.cover + '"></div></a>'+
                                '<div class="conl" style="width:60%;"><a href="info.html?id=' + item.id + '&type=' + parmType + '">' + item.title + '</a>' +
                                '<span>' + formatDate(item.publishDate)+ '</span>' +
                                '<div class="small" style="width:100%;">'
                                + item.description +'</div></div><div class=""></div></li>')
                        }
                        $('.small').each(function(){
                            var maxwidth=25;
                            if($(this).text().length > maxwidth){
                                $(this).text($(this).text().substring(0,maxwidth));
                                $(this).html($(this).html()+'...');
                            }
                        });

                        $(document).ready(function(){
                            $("#scrollDiv").Scroll({line:1,speed:500,timer:3000,up:"but_up",down:"but_down"});
                        });

                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            })

        } else {

            $.ajax({
                type: "GET",
                url: restUrl + "/index/exchange/" + id,   //接口地址
                dataType: "json",
                data: {},
                success: function (v) {
                    if (v.code == "10000") {

                        var item = v.data;
                        if(item.title.indexOf('\n') >=0){ item.title = item.title.replace(/\n/g,"<br/>");}

                        var str = '<h1 class="animated fadeInDown" style="text-align: center">' + item.title + '</h1><div class="animated fadeInUp" style="animation-delay: 0.4s"><p>'+ item.exContent + '</p><br/></div><div class="fj"></div>'+
                            '<div class="sign animated fadeInUp" style="animation-delay: 0.6s"><div class="phone"><img src="./images/telephone.png"></div><span>热线电话：' + item.orgPhone +
                            '</span><button type="button" class="baoming" id="meeting">我要报名</button></div><div class="link"><div class="link_1"><span>下一篇：</span><a href="info.html?id=' +
                            nextId + '&type=' + parmType + '">' + nextTitle + '</a></div><div class ="link_1"><span> 上一篇：</span><a href="info.html?id=' + preId + '&type=' + parmType + '">' +
                            preTitle + '</a></div></div>';
                        $('.news_del .main').html(str);

                        if(item.cmsFileList.length != 0){
                            $('.fj').addClass('xline');
                            for(var i=0;i<item.cmsFileList.length;i++){
                                $('.fj').append('<br/><a href="' + pre + item.cmsFileList[i].savePath + '" download="' + item.cmsFileList[i].reqRelUrl + '"><span style="font-weight: bold">附件' + (i+1) + '：</span>' + item.cmsFileList[i].fileName + '</a>');
                            }
                        }

                        var model =
                            '<!-- 模态框（Modal） -->' +
                            '<div class="modal fade" id="myModal" style="width: 100%;">' +
                            '    <div class="modal-dialog">' +
                            '        <div class="modal-content">' +
                            '            <div class="modal-header">' +
                            '                <h3 class="modal-title" id="myModalLabel">' +
                            '                    活动报名' +
                            '                </h3>' +
                            '            </div>' +
                            '            <div class="panel panel-info">' +
                            '                <div class="panel-body">' +
                            '                    <form class="form-horizontal" style="margin-top:20px">' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputName" class="col-md-12 control-label"><span style="color: red;">*</span>&nbsp;姓名</label>' +
                            '                            <div class="col-md-11">' +
                            '                                <input type="text" class="form-control input-sm" id="name" style="height:40px" placeholder="请输入姓名">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputWorkplace" class="col-sm-12 control-label"><span style="color: red;">*</span>&nbsp;工作单位</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <input type="text" class="form-control" id="workplace" style="height:40px" placeholder="请输入工作单位">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputJob" class="col-sm-12 control-label">职务</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <input type="text" class="form-control" id="job" style="height:40px" placeholder="请输入职务">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">\n' +
                            '                            <label for="inputPhone" class="col-sm-12 control-label"><span style="color: red;">*</span>&nbsp;联系电话</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <input type="text" class="form-control" id="phone" style="height:40px" placeholder="请输入联系电话">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputMail" class="col-sm-12 control-label">联系邮箱</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <input type="text" class="form-control" id="email" style="height:40px" placeholder="请输入联系邮箱">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputAddr" class="col-sm-12 control-label">联系地址</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <input type="text" class="form-control" id="addr" style="height:40px" placeholder="请输入联系地址">' +
                            '                            </div>' +
                            '                        </div>' +
                            '                        <div class="form-group">' +
                            '                            <label for="inputMemo" class="col-sm-12 control-label">备注</label>' +
                            '                            <div class="col-sm-11">' +
                            '                                <textarea class="form-control" rows="6" id="memo" placeholder="请输入备注"></textarea>' +
                            '                            </div>' +
                            '                        </div>' +
                            '                    </form>' +
                            '                </div>' +
                            '            </div>' +
                            '            <div class="row">' +
                            '                <div class="col-xs-3 col-sm-3 col-md-3">' +
                            '                </div>' +
                            '                <div class="col-xs-3 col-sm-3 col-md-3">' +
                            '                    <button type="button" class="btn btn-primary btn-block" style="height:40px" id="confirm">报名' +
                            '                    </button>' +
                            '                </div>' +
                            '                <div class="col-xs-3 col-sm-3 col-md-3">' +
                            '                    <button type="button" class="btn btn-danger btn-block" style="height:40px" id="cancel">返回' +
                            '                    </button>' +
                            '                </div>' +
                            '                <div class="col-xs-3 col-sm-3 col-md-3">' +
                            '                </div>' +
                            '            </div>' +
                            '        </div>' +
                            '    </div>' +
                            '</div>' +
                            '<!-- 模态框（Modal） end-->' +
                            '<!-- 模态框（Modal） -->' +
                            '<div class="modal fade" id="resultModal">' +
                            '    <div class="modal-dialog dialog">' +
                            '        <div class="modal-content">' +
                            '            <div class="modal-header">' +
                            '                <h3 class="modal-title" id="result_content">' +
                            '                    提交成功!' +
                            '                </h3>' +
                            '            </div>' +
                            '            <div class="modal-footer">' +
                            '                </button>' +
                            '                <button type="button" id="result_btn" class="btn btn-primary">' +
                            '                    确定' +
                            '                </button>' +
                            '            </div>' +
                            '        </div><!-- /.modal-content -->' +
                            '    </div><!-- /.modal -->' +
                            '</div>';
                        $('#footer').before(model);

                        var script = $('<script type="text/javascript" src="js/activity_info.js"></script>');
                        $('body').append(script);
                        buildUserInfo();
                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            })

            $.ajax({
                type: "GET",
                url: restUrl + "/index/exchanges",   //接口地址
                dataType: "json",
                data: {
                    loadAll: false,
                    needPaging: false,
                },
                success: function (v) {
                    if (v.code == "10000") {
                        for(var i=0;i<v.data.rows.length;i++){

                            var item = v.data.rows[i];
                            $('.dd').append('<li class="recommend"><a href="info.html?id=' + item.id + '&type=' + parmType + '"><div class="xx"><img src="' + pre + item.cover + '"></div></a>'+
                                '<div class="conl"><a href="info.html?id=' + item.id + '&type=' + parmType + '">' + item.title + '</a><span>' + formatDate(item.enrollStart) + '---' + formatDate(item.enrollEnd)+
                                '</span><div class="small">'+ item.subject +'</div></div><div class="clearfix"></div></li>')
                        }


                        $('.small').each(function(){
                            var maxwidth=25;
                            if($(this).text().length > maxwidth){
                                $(this).text($(this).text().substring(0,maxwidth));
                                $(this).html($(this).html()+'...');
                            }
                        });

                        $(document).ready(function(){
                            $("#scrollDiv").Scroll({line:1,speed:500,timer:3000,up:"but_up",down:"but_down"});
                        });

                    } else {
                        console.log(v.message)
                    }
                },
                error: function (data) {
                    console.log(data)
                }
            })

        }
    };

    //转换日期格式
    function formatDate(v) {
        if (/^(-)?\d{1,10}$/.test(v)) {
            v = v * 1000;
        } else if (/^(-)?\d{1,13}$/.test(v)) {
            v = v * 1;
        }
        var dateObj = new Date(v);
        var month = dateObj.getMonth() + 1;
        var day = dateObj.getDate();
        var hours = dateObj.getHours();
        var minutes = dateObj.getMinutes();
        var seconds = dateObj.getSeconds();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        <!--if(hours<10){-->
        <!--hours = "0" + hours;-->
        <!--}-->
        <!--if(minutes<10){-->
        <!--minutes = "0" + minutes;-->
        <!--}-->
        <!--if(seconds<10){-->
        <!--seconds = "0" + seconds;-->
        <!--}-->
        <!--var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;-->
        var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' + day + ' ';
        return UnixTimeToDate;
    }

});

